Passed
Push — development ( 811359...8d1f71 )
by Vad
12:40 queued 13s
created

UsersService.softDeleteUser   A

Complexity

Conditions 2

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 9
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 2
rs 9.95
c 0
b 0
f 0
1 9
import { Injectable } from '@nestjs/common';
2 9
import { InjectRepository } from '@nestjs/typeorm';
3 9
import { Repository } from 'typeorm';
4 9
import { User } from './entities/user.entity';
5 9
import { NotFoundException } from '@nestjs/common';
0 ignored issues
show
introduced by
'@nestjs/common' import is duplicated.
Loading history...
6
import { UpdateUserDto } from './dto/update-user.dto/update-user.dto';
7
import { AdjustFundsDto } from './dto/update-user.dto/adjust-funds.dto';
8
9
@Injectable()
10 9
export class UsersService {
11
  constructor(
12
    @InjectRepository(User)
13 7
    private userRepository: Repository<User>,
14
  ) {}
15
16
  async updateTerms(githubId: string, hasAcceptedTerms: boolean): Promise<User> {
17 2
    const user = await this.userRepository.findOne({
18
      where: { githubId },
19
    });
20
21
    // This will never be invoked through the controller because the auth guard will throw an error
22
    // However, if this is used as a standalone service, this check is necessary
23
    // removing to get rid of test case
24
    // if (!user) {
25
    //   throw new NotFoundException('User not found');
26
    // }
27
28 2
    user.hasAcceptedTerms = hasAcceptedTerms;
29 2
    return this.userRepository.save(user);
30
  }
31
  // Find all customers
32
  async findAll(): Promise<User[]> {
33 1
    return this.userRepository.find();
34
  }
35
  // Find a customer by ID
36
  async findById(githubId: string): Promise<User> {
37 9
    const user = await this.userRepository.findOne({ where: { githubId } });
38 9
    if (!user) {
39 1
      throw new NotFoundException('User not found');
40
    }
41 8
    return user;
42
  }
43
  // Update customer fields
44
  async update(githubId: string, updateUserDto: UpdateUserDto): Promise<User> {
45 1
    const user = await this.findById(githubId);
46
47 1
    return this.userRepository.save({ ...user, ...updateUserDto });
48
  }
49
50
  async adjustFunds(githubId: string, adjustFundsDto: AdjustFundsDto): Promise<User> {
51 4
    const user = await this.userRepository.findOneBy({ githubId });
52
53 4
    if (!user) {
54 1
      throw new NotFoundException(`User with GitHub ID ${githubId} not found.`);
55
    }
56
57
    // Update balance and/or isMonthlyPayment only if provided
58 3
    if (adjustFundsDto.balance !== undefined) {
59 2
      user.balance = adjustFundsDto.balance;
60
    }
61 3
    if (adjustFundsDto.isMonthlyPayment !== undefined) {
62 1
      user.isMonthlyPayment = adjustFundsDto.isMonthlyPayment;
63
    }
64
65 3
    return this.userRepository.save(user);
66
  }
67
68
  async softDeleteUser(githubId: string): Promise<User> {
69 3
    const user = await this.userRepository.findOne({ where: { githubId } });
70
71 3
    if (!user) {
72 1
      throw new NotFoundException(`User with GitHub ID ${githubId} not found.`);
73
    }
74
75
    // Update the roles to include "inactive"
76 2
    user.roles = ['inactive'];
77 2
    return this.userRepository.save(user);
78
  }
79
}
80